<!DOCTYPE html>
<html>
<head>
    <title>添加医生--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css"/>
    <link rel="stylesheet" type="text/css" href="../Css/style.css"/>
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/jquery.sorted.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="../Js/webUtil.js"></script>
    <style type="text/css">
        body {
            padding-bottom: 40px;
        }

        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
        $(function () {
            $('#backid').click(function () {
                window.location.href = "index.html";
            });
        });
    </script>
</head>
<body>
<form method="post" class="definewidth m20">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">姓名</td>
            <td><input type="text" name="name" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">证件号码</td>
            <td><input type="text" name="idNumber" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">手机</td>
            <td><input type="text" name="phone" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">座机</td>
            <td><input type="text" name="telephone" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">性别</td>
            <td>
                <input name="sex" readonly unselectable="on"/>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">出生年月</td>
            <td><input type="date" name="birthday" value="" readonly unselectable="on"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">年龄</td>
            <td><input name="age" readonly unselectable="on"/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">电子邮箱</td>
            <td><input type="text" name="email" value=""/></td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">所属科室</td>
            <td>
                <select name="departmentId">
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">学历</td>
            <td>
                <select name="degreeId">
                </select>
            </td>
        </tr>
        <tr>
            <td width="10%" class="tableleft">备注</td>
            <td><textarea name="comment"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <center>
                    <button type="button" class="btn btn-primary" id="save">保存</button> &nbsp;&nbsp;<button
                        type="button" class="btn btn-success" name="backid" id="backid">返回列表
                </button>
                </center>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>
    function loadData() {
        $.ajax({
            type: "GET",
            url: "/degree/list",
            success: function (vo) {
                if (vo.code == 200) {
                    let list = vo.list;
                    let str = "";
                    for (let i = 0; i < list.length; i++) {
                        str += "<option value='" + list[i].id + "'>" + list[i].name + "</option>";
                    }
                    $("select[name='degreeId']").html(str);
                } else
                    alert(vo.msg);
            }
        })

        $.ajax({
            type: "GET",
            url: "/department/list",
            success: function (vo) {
                if (vo.code == 200) {
                    let list = vo.list;
                    let str = "";
                    for (let i = 0; i < list.length; i++) {
                        str += "<option value='" + list[i].id + "'>" + list[i].name + "</option>";
                    }
                    $("select[name='departmentId']").html(str);
                } else
                    alert(vo.msg);
            }
        })

    }

    function updateByNumber() {
        let idNumber = $("input[name='idNumber']").val();
        if (!idNumber.isIdNumber()) {
            alert("请输入正确的身份证号!")
            return false;
        } else {
            let year = idNumber.substr(6, 4);
            let month = idNumber.substr(10, 2);
            let day = idNumber.substr(12, 2);
            let birthday = year + "-" + month + "-" + day;
            $("input[name='birthday']").val(year + "-" + month + "-" + day)

            let birthday_date = +new Date(birthday);
            $("input[name='age']").val(~~((Date.now() - birthday_date) / (31557600000)))

            let sex = "女";
            if (idNumber[16] % 2 == 1)
                sex = "男";
            $("input[name='sex']").val(sex)
            return true;
        }
    }

    $(function () {
        loadData();
        $("input[name='idNumber']").blur(() => updateByNumber())
        $("#save").click(
            function () {
                let name = $("input[name='name']").val();
                let phone = $("input[name='phone']").val();
                let telephone = $("input[name='telephone']").val();
                let email = $("input[name='email']").val();

                if (!name.isName()) {
                    alert("请输入正确的姓名!");
                    return;
                }
                if (!phone.isPhone()) {
                    alert("请输入正确的手机号码!");
                    return;
                }
                if (!telephone.isTelephone()) {
                    alert("请输入正确的座机号码!");
                    return;
                }
                if (!updateByNumber()) {
                    return;
                }

                if (!email.isEmail()) {
                    alert("请输入正确的邮箱!");
                    return;
                }

                $.ajax(
                    {
                        type: "POST",
                        url: "/doctors/",
                        data: $("form").serialize(),
                        success: function (vo) {
                            if (vo.code == 200) {
                                alert(vo.obj);
                                window.location.href = "index.html"
                            } else
                                alert(vo.msg);
                        }
                    }
                )
            }
        )
    })
</script>